package im.yisme.controler;

import android.util.Log;
import im.yisme.controler.service.GameCtrlService;
import im.yisme.model.service.GameSystemService;
import im.yisme.model.service.ModelServiceProvider;
import im.yisme.view.service.GameActivityService;
import im.yisme.view.service.GameViewService;
import im.yisme.vo.InputResultVO;
import im.yisme.vo.LyricsVO;
import im.yisme.vo.MusicVO;
import im.yisme.vo.RawActionVO;



public class GameCtrl implements GameCtrlService{

	private static GameCtrl ctrl;
	private GameSystemService service;
	private boolean quitFlag;
	private GameViewService view;
	private GameActivityService activity;
	
	private GameCtrl(){
		service = ModelServiceProvider.getGameSystemService();
	}
	
	public static GameCtrl getInstance(){
		return ctrl == null ? ctrl = new GameCtrl() : ctrl;
	}
	

	public void setPlayingMusic(long musicId) {
		// TODO Auto-generated method stub
		MusicCtrl musicCtrl = MusicCtrl.getInstance();
		MusicVO playingMusic = musicCtrl.findMusic(musicId);
		
		service.setPlayingMusic(playingMusic);
	}


	public void setPlayingLyrics(long lyricsId) {
		// TODO Auto-generated method stub
		LyricsCtrl lyricsCtrl = LyricsCtrl.getInstance();
		LyricsVO playingLyrics = lyricsCtrl.findLyrics(lyricsId);
		
		service.setPlayingLyrics(playingLyrics);
	}

	
	public void reset() {
		// TODO Auto-generated method stub
		service.setPlayingLyrics(null);
		service.setPlayingMusic(null);
		service.reset();
	}


	public void loadResource() {
		// TODO Auto-generated method stub
		
		service.loadResource();
		
	}


	public void startGame() {
		// TODO Auto-generated method stub

	}


	public void pause() {
		// TODO Auto-generated method stub
		boolean isRunning = service.isRunning();
		service.setRunning(!isRunning);
	}


	public void quit() {
		// TODO Auto-generated method stub
		quitFlag = true;
	}
	
	private void endGame(){

	}

	public void input(RawActionVO action) {
		// TODO Auto-generated method stub
		InputResultVO result=service.handleInput(action);
		
		if(result.isSuccess()){
			view.validTap(result.getWhichPillar());
			
			if(result.getLevel().length() != 0) view.levelResult(result.getLevel());
			
		}
		
		activity.updateCombo();
		activity.updateScore();
	}
	public void setView(GameViewService view){
		this.view = view;
	}
	public void setActivity(GameActivityService activity){
		this.activity = activity;
	}

	public void finishGame() {
		// TODO Auto-generated method stub
		
	}

	public void checkOverdue(int moment) {
		// TODO Auto-generated method stub
		boolean flag = service.checkOverdue(moment);
		
		if(flag) view.levelResult("miss");
	}
}
